import { checkLoginReq } from '@/api/user'

const state = {
  role: localStorage.getItem('role'),
  token: localStorage.getItem('t_k')
}

const mutations = {
  SET_TOKEN(state, data) {
    state.token = data
    localStorage.setItem('t_k', data)
  },
  SET_ROLE(state, data) {
    state.role = data
    localStorage.setItem('role', data)
  }
}

const actions = {
  A_Login({ commit }, data) {
    return new Promise(async resolve => {
      let res = await checkLoginReq(data)
      let { code, role, token } = res.data
      if (code == 0) {
        commit('SET_TOKEN','Bearer '+token)
        commit('SET_ROLE',role)
        resolve()
      }
    })
  }
}

//导出模块
export default {
  namespaced: true,
  state,
  mutations,
  actions,
}